package com.eascs.web.o2o.scheduler.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;



public class DateUtil {
	/** 
     * 默认日期格式 
     */  
    public static String DEFAULT_FORMAT = "yyyy-MM-dd"; 
    
	 public static String getSpecifiedDayBefore(int n) {//可以用new Date().toLocalString()传递参数  
	        Calendar c = Calendar.getInstance();  
	        Date date = new Date();  
	        c.setTime(date);  
	        int day = c.get(Calendar.DATE);  
	        c.set(Calendar.DATE, day - n);  
	  
	        String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c  
	                .getTime());  
	        return dayBefore;  
	    }  
	 
	 
	 public static Date getSpecifiedDayBeforeDate(int n) {//可以用new Date().toLocalString()传递参数  
	        Calendar c = Calendar.getInstance();  
	        Date date = new Date();  
	        c.setTime(date);  
	        int day = c.get(Calendar.DATE);  
	        c.set(Calendar.DATE, day - n);  
	        return c.getTime();  
	    } 
	 
	 public static String formatDate(Date date){  
	        SimpleDateFormat f = new SimpleDateFormat(DEFAULT_FORMAT);  
	        String sDate = f.format(date);  
	        return sDate;  
	    }  
	      
	    /** 
	     * 获取当年的第一天 
	     * @param year 
	     * @return 
	     */  
	    public static Date getCurrYearFirst(){  
	        Calendar currCal=Calendar.getInstance();    
	        int currentYear = currCal.get(Calendar.YEAR);  
	        return getYearFirst(currentYear);  
	    }  
	      
	    /** 
	     * 获取当年的最后一天 
	     * @param year 
	     * @return 
	     */  
	    public static Date getCurrYearLast(){  
	        Calendar currCal=Calendar.getInstance();    
	        int currentYear = currCal.get(Calendar.YEAR);  
	        return getYearLast(currentYear);  
	    }  
	    /** 
	     * 获取上年的第一天 
	     * @param year 
	     * @return 
	     */  
	    public static Date getYeastyearFrist(){  
	        Calendar currCal=Calendar.getInstance();    
	        int currentYear = currCal.get(Calendar.YEAR);  
	        return getYearFirst(currentYear-1);  
	    }  
	      
	    /** 
	     * 获取上年的最后一天 
	     * @param year 
	     * @return 
	     */  
	    public static Date getYeastyearLast(){  
	        Calendar currCal=Calendar.getInstance();    
	        int currentYear = currCal.get(Calendar.YEAR);  
	        return getYearLast(currentYear-1);  
	    } 
	    /** 
	     * 获取某年第一天日期 
	     * @param year 年份 
	     * @return Date 
	     */  
	    public static Date getYearFirst(int year){  
	        Calendar calendar = Calendar.getInstance();  
	        calendar.clear();  
	        calendar.set(Calendar.YEAR, year);  
	        Date currYearFirst = calendar.getTime();  
	        return currYearFirst;  
	    }  
	      
	    /** 
	     * 获取某年最后一天日期 
	     * @param year 年份 
	     * @return Date 
	     */  
	    public static Date getYearLast(int year){  
	        Calendar calendar = Calendar.getInstance();  
	        calendar.clear();  
	        calendar.set(Calendar.YEAR, year);  
	        calendar.roll(Calendar.DAY_OF_YEAR, -1);  
	        Date currYearLast = calendar.getTime();  
	          
	        return currYearLast;  
	    }  
	  public static void main(String[] args) {
		  System.out.println( getSpecifiedDayBeforeDate(-1));
		 System.out.println(getCurrYearFirst());
		 System.out.println(getCurrYearLast());
		 System.out.println(getYeastyearFrist());
		 System.out.println(getYeastyearLast());

	}

}
